<?php
/**
 * @name /www/unit-tests/User.php
 * @author Michael Foss <michael@mikefoss.com>
 * @version 2011.03.26
 *
 * Unit test for User class.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
// Libraries
ReqLib('/lib/private/User.php');

function DefineHeader() {
?><style type="text/css">
  code {
    font: bold 10pt 'Courier New';
    color: #666;
    background-color: #CCC;
    border: 3px solid #666;
    padding: 4px 6px;
    margin: 10px 20px;
    display: block;
  }
  form, ul, ol {
    margin: 0px 20px;
  }
  a#Back {
    float: right;
    margin: 10px;
    padding: 4px 6px;
    border: 1px solid #666;
    background-color: #CCC;
  }
</style><?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
/*
  "GET":
  User[] GetUsers(int offset, int maxResults)
  bool Exists(int userId)
  string ToString()

  "SCUD":
  bool Load(int userId)
  bool Create(userName, email)
  bool Save()
  static bool Delete(int userId)

  Custom:
  int GetUserIdByUserName(string userName)
  int GetUserIdByEmail(string email)
  bool SetPassword(string password)
  bool ValidatePassword(string password)
  bool HasRole(string role)
  bool GrantRole(string role)
  bool RevokeRole(string role)
  static string[] GetRoles()
  static bool CreateRole(string name, string description)
  static bool DeleteRole(string name)
*/
  $actions = array(
    'Delete(id)',
    'Delete All',
    'Create',
    'View Details',
    'List(offset,max)',
    'GetIdByUserName(name)',
    'GetIdByEmail(email)',
    'Save',
    'HasRole(userid,role)',
    'GrantRole(userid,role)',
    'RevokeRole(userid,role)',
    'GetRoles',
    'CreateRole(name,desc)',
    'DeleteRole(name)',
    'SetPassword(userid,pw)',
    'ValidatePassword(userid,pw)'
  );
  // Page content goes here.
  ?>
<h1><a href="User.php">User - Unit Test</a></h1>
<a id="Back" href="index.php">Back to Unit Testing Home</a>
<form method="GET">
  <label for="Param1">Param 1: </label><input type="text" value="<?=htmlspecialchars($_GET['Param1']) ?>" name="Param1" /><br />
  <label for="Param2">Param 2: </label><input type="text" value="<?=htmlspecialchars($_GET['Param2']) ?>" name="Param2" /><br />
<?
  foreach ($actions as $action)
    println('<input type="submit" name="Action" value="' . $action . '" /><br />');
?><br />
<hr />
<?
  DoAction();
?></form>
<?
}

function DoAction() {
?><p>
<?
  switch ($_GET['Action']) {
    case 'Delete(id)':
      println(User::Delete($_GET['Param1']) ? 'Deleted' : 'Not deleted');
      break;

    case 'Delete All':
      $us = User::GetUsers();
      foreach ($us as $u)
        User::Delete($u->UserId);
      println('Data cleared.');
      break;

    case 'Create':
      $u = new User();
      $us = array(
        array('', '', ''),
        array('1', '1', rand(1, 5)),
        array('2', '2', rand(1, 5)),
        array('3', '1', rand(1, 5)),
        array('1', '3', rand(1, 5)),
        array(rand(1, 5), rand(1, 5), rand(1, 5))
      );
      foreach ($us as $data)
        println("Creating user (un:'{$data[0]}'/em:'{$data[1]}'/pw:'{$data[2]}'): " . ($u->Create($data[0], $data[1], $data[2]) ? 'Created' : 'Not created') . '<br />');
      break;

    case 'List(offset,max)':
      println('<ol>');
      foreach(User::GetUsers($_GET['Param1'], $_GET['Param2']) as $u)
        println("<li>{$u->UserId}: {$u->UserName}</li>");
      println('</ol>');
      break;

    case 'View Details':
      println('<textarea style="width: 600px; height: 400px;">' . print_r(User::GetUsers(0, 1), true) . '</textarea>');
      break;

    case 'GetIdByUserName(name)':
      println("ByName('" . htmlspecialchars($_GET['Param1']) . "') = '" . User::GetUserIdByUserName($_GET['Param1']) . "'");
      break;

    case 'GetIdByEmail(email)':
      println("ByEmail('" . htmlspecialchars($_GET['Param1']) . "') = '" . User::GetUserIdByEmail($_GET['Param1']) . "'");
      break;

    case 'Save':
      $u = User::GetUsers(0, 1);
      if (!count($u)) {
        $u = new User();
        $u->Create('1', '1', '1');
      }
      else
        $u = $u[0];
      $u->UserName = rand(1, 100);
      $pw = rand(1, 100);
      $u->SetPassword($pw);
      $u->FirstName = rand(1, 100);
      $u->LastName = rand(1, 100);
      $u->PhoneNumber = rand(1, 100);
      $u->Street1 = rand(1, 100);
      $u->Street2 = rand(1, 100);
      $u->Street3 = rand(1, 100);
      $u->City = rand(1, 100);
      $u->State = rand(1, 100);
      $u->Country = rand(1, 100);
      $u->PostalCode = rand(1, 100);
      $u->Notes = rand(1, 100);
      println($u->Save() ? 'Saved' : 'Not saved');
      println('<br /><textarea style="width: 600px; height: 400px;">pw: ' . $pw . '\r\n' . print_r($u->ToString("\r\n"), true) . '</textarea>');
      break;

    case 'HasRole(userid,role)':
      $u = new User();
      if (!$u->Load($_GET['Param1']))
        println('Could not load');
      else
        println($u->HasRole($_GET['Param2']) ? 'Has role' : 'Does not have role');
      break;

    case 'GrantRole(userid,role)':
      $u = new User();
      if (!$u->Load($_GET['Param1']))
        println('Could not load');
      else
        println($u->GrantRole($_GET['Param2']) && $u->Save() ? 'Granted' : 'Not granted');
      break;

    case 'RevokeRole(userid,role)':
      $u = new User();
      if (!$u->Load($_GET['Param1']))
        println('Could not load');
      else
        println($u->RevokeRole($_GET['Param2']) && $u->Save() ? 'Revoked' : 'Not revoked');
      break;

    case 'GetRoles':
      $roles = User::GetRoles();
      println('<br /><textarea style="width: 600px; height: 400px;">' . print_r($roles, true) . '</textarea>');
      break;

    case 'CreateRole(name,desc)':
      println(User::CreateRole($_GET['Param1'], $_GET['Param2']) ? 'Created' : 'Not created');
      break;

    case 'DeleteRole(name)':
      println(User::DeleteRole($_GET['Param1']) ? 'Deleted' : 'Not deleted');
      break;

    case 'SetPassword(userid,pw)':
      $u = new User();
      if (!$u->Load($_GET['Param1']))
        println('Could not load');
      else {
        $u->SetPassword($_GET['Param2']);
        println($u->Save() ? 'Password set' : 'Password not set');
      }
      break;

    case 'ValidatePassword(userid,pw)':
      $u = new User();
      if (!$u->Load($_GET['Param1']))
        println('Could not load');
      else
        println($u->ValidatePassword($_GET['Param2']) ? 'Validated' : 'Not validated');
      break;

    default:
      break;
  }
?></p>
<?
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');